package com.example.springboot_ruiji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_ruiji.common.R;
import com.example.springboot_ruiji.constans.EmployeeConstans;
import com.example.springboot_ruiji.dao.SetMealDao;
import com.example.springboot_ruiji.dao.SetMealDtoDao;
import com.example.springboot_ruiji.dao.SetmealDishDao;
import com.example.springboot_ruiji.pojo.*;
import com.example.springboot_ruiji.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private SetmealDishDao setmealDishDao;

    @Autowired
    private SetMealDtoDao setMealDtoDao;

    @Override
    public R page(String name, Integer page, Integer pageSize) {
        Page<Dish> page1=new Page<>(page,pageSize);
        //判断是否为空
        log.info(name);
        Page<DishDTO> dishPage=setMealDao.selectDishPage(name,page1);
        return R.success(dishPage);
    }


    @Transactional
    @Override
    public R save(SetMealDto setMealDto) {
        setMealDto.setCreateTime(LocalDateTime.now());
        setMealDto.setUpdateTime(LocalDateTime.now());
        setMealDao.insert(setMealDto);

        List<SetmealDish> setmealDishes = setMealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setMealDto.getId());
            setmealDish.setCreateTime(setMealDto.getCreateTime());
            setmealDish.setUpdateTime(setMealDto.getUpdateTime());
            setmealDish.setUpdateUser(setMealDto.getUpdateUser());
            setmealDish.setCreateUser(setMealDto.getCreateUser());
        }
        for (SetmealDish setmealDish : setmealDishes) {
            setMealDtoDao.insert(setmealDish);
        }
        return R.success("添加成功");
    }

    @Override
    public SetMealDto findById(Long id) {

        Setmeal setmeal = setMealDao.selectById(id);

        List<SetmealDish> setmealDishList = setMealDtoDao.findByDishId(id);

        SetMealDto setMealDto = new SetMealDto();

        BeanUtils.copyProperties(setmeal,setMealDto);

        setMealDto.setSetmealDishes(setmealDishList);

        return setMealDto;
    }

    @Override
    public R batchDelete(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return R.error("参数不能为空");
        }
        String[] setMealIds = ids.split(",");
        for (String setMealId : setMealIds) {
            //逻辑删除菜品信息表中的数据
            Setmeal setmeal = new Setmeal();
            setmeal.setId(Long.valueOf(setMealId));
            setmeal.setIsDeleted(1);
            setmeal.setUpdateTime(LocalDateTime.now());
            setmeal.setUpdateUser(1l);
            setMealDao.deleteById(setmeal);
            //逻辑删除口味表中的数据
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(1l);
            setmealDish.setIsDeleted(1);
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, setMealId);
            setMealDtoDao.delete(queryWrapper);
        }
        return R.success("删除成功");
    }

    @Transactional
    @Override
    public void update(SetMealDto setMealDto) {
        setMealDto.setUpdateTime(LocalDateTime.now());
        //2.更新菜品
        setMealDao.updateset(setMealDto);

        //3.删除菜品的口味
        setmealDishDao.deleteByDishId(setMealDto.getId());

        //4. 得到当前菜品最新口味，然后补全数据
        List<SetmealDish> setmealDishes = setMealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setMealDto.getId());
            setmealDish.setCreateTime(setMealDto.getCreateTime());
            setmealDish.setUpdateTime(setMealDto.getUpdateTime());
            setmealDish.setUpdateUser(setMealDto.getUpdateUser());
            setmealDish.setCreateUser(setMealDto.getCreateUser());
        }
        for (SetmealDish setmealDish : setmealDishes) {
            setMealDtoDao.insert(setmealDish);
        }
    }

    @Override
    public R dishStatusByStatus(SetMealDto setMealDto, String ids, Integer status) {
        if (Optional.ofNullable(setMealDto).isPresent()){
            LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (Optional.ofNullable(setMealDto.getId()).isPresent()&&Optional.ofNullable(setMealDto.getStatus()).isPresent()){
                lambdaQueryWrapper.eq(Setmeal::getId,setMealDto.getId());
            }
            //获取id、状态
            String[] idss = ids.split(",");
            //循环获取数组值
            for (int i = 0; i < idss.length; i++) {
                //赋值到对象中
                Setmeal setmeal = new Setmeal();
                setmeal.setUpdateTime(LocalDateTime.now());
                setmeal.setUpdateUser(setMealDto.getUpdateUser());
                Long id = Long.valueOf(idss[i]);
                setmeal.setId(id);
                setmeal.setStatus(status);
                //循环修改状态
                setMealDao.updateById(setmeal);
            }
            return R.success(EmployeeConstans.DISH_STATUSSUCCESS);
        }
        return R.success(EmployeeConstans.DISH_STATUSERROR);
    }
}
